#include "smbManager.h"
#ifdef Q_OS_ANDROID
SMBManager::SMBManager(){

}

SMBManager& SMBManager::I(){
    static SMBManager instance;
    return instance;
}

QStringList SMBManager::getIpList(){
    QStringList res;
    foreach(auto info, _connectMap){
        res.append(info.ip);
    }
    return res;
}

bool SMBManager::loadConnection(){

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    sql = "Select Ip, Username, Password from SMBInfo";
    query.exec(sql);
    while(query.next()){
        QString ip = query.value("Ip").toString();
        QString usr = query.value("Username").toString();
        QString pwd = query.value("Password").toString();

        // 从数据库导入的数据直接存入SMB
        _connectMap.insert(ip, ConnectInfo(ip, usr, pwd));
    }
    return true;
}

bool SMBManager::addConnection(QString ip, QString usr, QString pwd){
    if(JNIManager::I().getFileList_SMB(ip, usr, pwd, "/").size() == 0){
        return false;
    }
    ConnectInfo ci(ip, usr, pwd);
    _connectMap.insert(ip, ci);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("Replace into SMBInfo values('%1', '%2', '%3')").arg(ip, usr, pwd);
    query.exec(sql);
    return true;
}


bool SMBManager::delConnection(QString ip){
    if(_connectMap.contains(ip)){
        _connectMap.remove(ip);
        return true;
    }else{
        return false;
    }
}

QStringList SMBManager::getFileList(QString ip, QString path){
    QStringList res;
    if(! _connectMap.contains(ip)){
        return res;
    }
    QString usr = _connectMap[ip].usr;
    QString pwd = _connectMap[ip].pwd;

    res = JNIManager::I().getFileList_SMB(ip, usr, pwd, path);

    return res;
}

bool SMBManager::uploadFile(QString SMBPath, QString localPath){

    QString ip, path;
    splitSMBPath(SMBPath, ip, path);

    if(! _connectMap.contains(ip)){
        return false;
    }
    QString usr = _connectMap[ip].usr;
    QString pwd = _connectMap[ip].pwd;

    return JNIManager::I().uploadFile_SMB(ip, usr, pwd, path, localPath);

}

bool SMBManager::uploadFile(QString ip, QString remotePath, QString localPath){

    if(! _connectMap.contains(ip)){
        return false;
    }
    QString usr = _connectMap[ip].usr;
    QString pwd = _connectMap[ip].pwd;

    return JNIManager::I().uploadFile_SMB(ip, usr, pwd, remotePath, localPath);

}

bool SMBManager::downloadFile(QString SMBPath, QString localPath){

    QString ip, path;
    splitSMBPath(SMBPath, ip, path);

    if(! _connectMap.contains(ip)){
        return false;
    }
    QString usr = _connectMap[ip].usr;
    QString pwd = _connectMap[ip].pwd;

    return JNIManager::I().downloadFile_SMB(ip, usr, pwd, path, localPath);

}

bool SMBManager::downloadFile(QString ip, QString remotePath, QString localPath){

    if(! _connectMap.contains(ip)){
        return false;
    }
    QString usr = _connectMap[ip].usr;
    QString pwd = _connectMap[ip].pwd;

    return JNIManager::I().downloadFile_SMB(ip, usr, pwd, remotePath, localPath);

}

QString SMBManager::createSMBPath(QString ip, QString path){
    ip.remove('/');
    path.replace("//", "/");
    return "SMB://" + ip + "/" + path;
}
void    SMBManager::splitSMBPath(QString SMBPath, QString& ip, QString& path){
    SMBPath.remove("SMB://");
    ip = SMBPath.split("/")[0];
    path = SMBPath.remove(ip);
}

QString SMBManager::SMB2Local(QString SMBPath){
    if(!SMBPath.contains("SMB://")){
        return SMBPath;
    }

    QString filename = SMBPath.split('/').back();
    QString localDir = JNIManager::I().getAppPath();    // 缓存至系统文件夹(?)

    return localDir + '/' + filename;

}

bool SMBManager::isSMBPath(QString SMBPath){
    return SMBPath.contains("SMB://");
}


#endif
